/** 
 * Project Name: farm-core 
 * File Name: BuguCallable.java 
 * Package Name: com.bugull.farm.core.frame.pool
 * Date: 2017年3月9日下午2:34:27 
 * Copyright (c) 2017, hadlinks All Rights Reserved. 
 * 
 */
package com.bugull.farm.core.frame.pool;

import java.util.UUID;
import java.util.concurrent.Callable;

/** 
 * ClassName: BuguCallable 
 * Function: 抽象Callable，自带任务执行状态度量器，可以生成任务执行潜在时间报表.
 * date: 2017年3月9日 下午2:34:27  
 * 
 * @author songwei (songw@hadlinks.com)
 * @version 
 * @since JDK 1.8 
 */
public abstract class BuguCallable<T> implements Callable<T>{

	//任务的唯一标识
	private final String id = UUID.randomUUID().toString();
	
	//任务的创建时间
	private final long time = System.currentTimeMillis();
	
	//任务执行依赖的线程池
	private AbstractSmartPool pool;
	
	//任务的特殊属性，决定是否进行失败重试
	private boolean retry = true;
	
	//任务的特殊属性，决定失败重试最大次数
	private Integer maxRetryTime = 3;
	
	//任务度量器
	private final ProjectionMetrics metrics;
	
	/**
	 * 默认构造函数
	 *	任务失败时自动重试，最大重试次数为3次
	 * @param pool
	 */
	public BuguCallable(AbstractSmartPool pool, ProjectionMetrics metrics){
		this.pool = pool;
		this.metrics = metrics;
	}
	
	/**
	 * 构造函数
	 * 可以指定任务是否进行失败重试，如果retry为false表示不需要重试，maxRetryTimes填null
	 * @param pool
	 * @param retry
	 * @param maxRetryTimes
	 */
	public BuguCallable(AbstractSmartPool pool, boolean retry, Integer maxRetryTimes, ProjectionMetrics metrics){
		this.pool = pool;
		this.retry = retry;
		this.maxRetryTime = maxRetryTimes;
		this.metrics = metrics;
	}
	
	
	@Override
	public abstract T call() throws Exception;

	public String getId() {
		return id;
	}

	public long getTime() {
		return time;
	}

	public boolean isRetry() {
		return retry;
	}

	public int getMaxRetryTime() {
		return maxRetryTime;
	}

	public AbstractSmartPool getPool() {
		return pool;
	}

	public ProjectionMetrics getMetrics() {
		return metrics;
	}
	
}
